/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.goods.appweb;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jeesite.modules.pay.totalPay.util.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jeesite.common.responseReturn.entity.ResultObject;
import com.jeesite.common.responseReturn.utils.CommonUtils;
import com.jeesite.common.responseReturn.web.Render;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.config.MyProps;
import com.jeesite.modules.goods.entity.ZtAddr;
import com.jeesite.modules.goods.entity.ZtGoods;
import com.jeesite.modules.goods.entity.ZtGoodscoll;
import com.jeesite.modules.goods.entity.ZtMsg;
import com.jeesite.modules.goods.service.ZtAddrService;
import com.jeesite.modules.goods.service.ZtGoodsService;
import com.jeesite.modules.goods.service.ZtGoodscollService;
import com.jeesite.modules.goods.service.ZtMsgService;
import com.jeesite.modules.jdProduct.utils.ProductPoolUtils;
import com.jeesite.modules.xcxAdmin.entity.ZtAdmin;

/**
 * 测试数据Controller
 * 
 * @author ThinkGem
 * @version 2018-04-22
 */
@Controller
@RequestMapping(value = "${frontPath}/ztAddr")
public class AppZtAddrController extends BaseController {
	
	@Autowired
	private ZtAddrService ztAddrService;
	@Autowired
	private ZtGoodscollService ztGoodscollService;
	@Autowired
	private ZtGoodsService ztGoodsService;
	
	@Autowired
	private ZtMsgService ztMsgService;
	
	@Autowired
	private MyProps myProps;
	
	/**
	 * 查询列表数据
	 */
	@RequestMapping(value = "listMsg")
	@ResponseBody
	public void listMsg( HttpServletRequest request,
			HttpServletResponse response,Model model) {
		
		String paramStr=CommonUtils.getParams(request);
		
		JSONObject jo=JSONObject.parseObject(paramStr);
		String adminid = jo.getString("adminid");
		String pageNum = jo.getString("pageNum");
		if(StringUtils.isBlank(pageNum)){
			pageNum="1";
		}
		ZtMsg ztMsg=new ZtMsg();
		ztMsg.setPageNum((1-Integer.valueOf(pageNum))*20);
		ztMsg.setAdminid(adminid);
		List<ZtMsg> page = ztMsgService.findMh(ztMsg);

		ResultObject res = new ResultObject();
		
		res.setResult(200L);
		res.setData(page);
		Render.renderString(response, res);
		
	}
	
	/**
	 * 查询列表数据
	 */
	
	@RequestMapping(value = "listData")
	@ResponseBody
	public void listData( HttpServletRequest request,
			HttpServletResponse response,Model model) {
		
		
		String paramStr=CommonUtils.getParams(request);
		
		JSONObject jo=JSONObject.parseObject(paramStr);
		String adminid = jo.getString("adminid");
		String pageNum = jo.getString("pageNum");
		if(StringUtils.isBlank(pageNum)){
			pageNum="1";
		}
		
		ZtGoodscoll ztGoodscoll=new ZtGoodscoll();
		
		ZtAdmin ztAdmin=new ZtAdmin();
		ztAdmin.setId(adminid);
		ztGoodscoll.setZtAdmin(ztAdmin);
		ztGoodscoll.setPageNum((Integer.valueOf(pageNum)-1)*15);
		List<ZtGoodscoll> page = ztGoodscollService.findMh(ztGoodscoll);
		List<ZtGoodscoll> ztList = new ArrayList<ZtGoodscoll>();
		
		for (ZtGoodscoll zt : page) {
			ZtGoods zd = zt.getZtGoods();
			String img = "";
			try {
				img = myProps.getServerPath() + ztGoodsService.getImgFirst(zd);
			} catch (Exception ex) {
				img = "";
			}
			zd.setImg(img);
			
			zt.setZtGoods(zd);
			
			ztList.add(zt);
		}
		
		
		ResultObject res = new ResultObject();
		
		res.setResult(200L);
		res.setData(page);
		Render.renderString(response, res);
		
	}
	
	/**
	 * 保存数据
	 */

	@PostMapping(value = "addColl")
	@ResponseBody
	public void addColl(HttpServletRequest request, HttpServletResponse response) {
		
		String paramStr=CommonUtils.getParams(request);
		
		JSONObject jo=JSONObject.parseObject(paramStr);
		String adminid = jo.getString("adminid");
		
		String goodsid = jo.getString("goodsid");
		
		ResultObject res = new ResultObject();
		
		ZtGoodscoll ztGoodscoll=new ZtGoodscoll();
		ZtAdmin ztAdmin=new ZtAdmin();
		ztAdmin.setId(adminid);
		ztGoodscoll.setZtAdmin(ztAdmin);
		ztGoodscoll.setCreateDate(new Date());
		ZtGoods ztGoods=new ZtGoods();
		ztGoods.setId(goodsid);
		ztGoodscoll.setZtGoods(ztGoods);	
		ztGoodscollService.save(ztGoodscoll);	
		res.setResult(200L);
		Render.renderString(response, res);
	}
	/**
	 * 保存数据
	 */

	@PostMapping(value = "deleteColl")
	@ResponseBody
	public void deleteColl(HttpServletRequest request, HttpServletResponse response) {
		
		String paramStr=CommonUtils.getParams(request);
		
		JSONObject jo=JSONObject.parseObject(paramStr);
		String id = jo.getString("id");	
		
		ResultObject res = new ResultObject();
		
		ZtGoodscoll ztGoodscoll=new ZtGoodscoll();
		ztGoodscoll.setId(id);
		ztGoodscollService.delete(ztGoodscoll);	
		res.setResult(200L);
		Render.renderString(response, res);
	}

	/**
	 * 查询列表
	 */
	@ResponseBody
	@RequestMapping(value = { "list", "" })
	public void list(HttpServletRequest request, HttpServletResponse response) {
			
		String paramStr=CommonUtils.getParams(request);
		
		JSONObject jo=JSONObject.parseObject(paramStr);
		String adminid = jo.getString("adminid");
		
		ResultObject res = new ResultObject();
		
		ZtAddr ztAddr=new ZtAddr();
		ZtAdmin ztAdmin=new ZtAdmin();
		ztAdmin.setId(adminid);
		ztAddr.setZtAdmin(ztAdmin);
		
		List<ZtAddr> addrList=ztAddrService.findAddrList(ztAddr);
		
		res.setResult(200L);
		res.setData(addrList);
		Render.renderString(response, res);
	}
	
	/**
	 * 查询列表
	 */
	@ResponseBody
	@RequestMapping(value = "getDefaultAddr")
	public void getDefaultAddr(HttpServletRequest request, HttpServletResponse response) {
			
		String paramStr=CommonUtils.getParams(request);
		
		JSONObject jo=JSONObject.parseObject(paramStr);
		String adminid = jo.getString("adminid");
		
		ResultObject res = new ResultObject();
		
		ZtAddr ztAddr=new ZtAddr();
		ZtAdmin ztAdmin=new ZtAdmin();
		ztAdmin.setId(adminid);
		ztAdmin.setStatus("0");
		ztAddr.setDefStatus("1");
		
		ztAddr.setZtAdmin(ztAdmin);
		
		ZtAddr addr=ztAddrService.getDefaultAddr(ztAddr);
		
		res.setResult(200L);
		res.setData(addr);
		Render.renderString(response, res);
	}

	/**
	 * 保存数据
	 */

	@PostMapping(value = "save")
	@ResponseBody
	public void save(HttpServletRequest request, HttpServletResponse response) {
		
		String paramStr=CommonUtils.getParams(request);
		
		JSONObject jo=JSONObject.parseObject(paramStr);
		String adminid = jo.getString("adminid");
		
		String name = jo.getString("name");
		String phone = jo.getString("phone");
		
		String detailaddr = jo.getString("detailaddr");
		String area = jo.getString("area");
		
		String province = "";
		if(jo.get("province")!=null){
			province=jo.get("province").toString();
		}
		String city = "";
		if(jo.get("city")!=null){
			city=jo.get("city").toString();
		}
		String county = "";
		if(jo.get("county")!=null){
			county=jo.get("county").toString();
		}
		String town = "";
		if(jo.get("town")!=null){
			town=jo.get("town").toString();
		}
		int townbid = 0;
		if(jo.get("townbid")!=null){
			townbid=Integer.valueOf(jo.get("townbid").toString());
		}
		int citybid = 0;
		if(jo.get("citybid")!=null){
			citybid=Integer.valueOf(jo.get("citybid").toString());
		}
		int provincebid = 0;
		if(jo.get("provincebid")!=null){
			provincebid=Integer.valueOf(jo.get("provincebid").toString());
		}
		int countybid = 0;
		if(jo.get("countybid")!=null){
			countybid=Integer.valueOf(jo.get("countybid").toString());
		}
		
		String defStatus="0";
		
		ResultObject res = new ResultObject();
		
		ZtAddr ztAddr=new ZtAddr();
		ZtAdmin ztAdmin=new ZtAdmin();
		ztAdmin.setId(adminid);
		ztAddr.setZtAdmin(ztAdmin);
		ztAddr.setDetailaddr(detailaddr);
		ztAddr.setArea(area);
		ztAddr.setProvince(province);
		ztAddr.setCity(city);
		ztAddr.setCounty(county);
		ztAddr.setTown(town);
		ztAddr.setProvincebid(provincebid);
		ztAddr.setCitybid(citybid);
		ztAddr.setCountybid(countybid);
		ztAddr.setTownbid(townbid);
		ztAddr.setCreateDate(new Date());
		int count=ztAddrService.findCountNum(ztAddr);
		 
		if(count==0){
			defStatus="1";
		}
		
		ztAddr.setDefStatus(defStatus);
		ztAddr.setAddr(area+" "+detailaddr);
		ztAddr.setName(name);
		ztAddr.setPhone(phone);
		
		ztAddrService.save(ztAddr);
		if("0".equals(ztAddr.getDefStatus())){
			if(jo.get("defStatus")!=null){
				defStatus=jo.get("defStatus").toString();
			if("1".equals(defStatus)){
				ztAddrService.setDefStatus(ztAddr);
				ztAddr.setDefStatus("1");
			}
			}
		}
		
		res.setData(ztAddr);
		res.setResult(200L);
		Render.renderString(response, res);
	}
	
	/**
	 * 保存数据
	 */

	@PostMapping(value = "updateAddr")
	@ResponseBody
	public void updateAddr(HttpServletRequest request, HttpServletResponse response) {
		
		String paramStr=CommonUtils.getParams(request);
		
		JSONObject jo=JSONObject.parseObject(paramStr);
		String addrid = jo.getString("addrid");
		
		String detailaddr = jo.getString("detailaddr");
		String name = jo.getString("name");
		String phone = jo.getString("phone");
		String area=jo.getString("area");
		
		String province = "";
		if(jo.get("province")!=null){
			province=jo.get("province").toString();
		}
		String city = "";
		if(jo.get("city")!=null){
			city=jo.get("city").toString();
		}
		String county = "";
		if(jo.get("county")!=null){
			county=jo.get("county").toString();
		}
		String town = "";
		if(jo.get("town")!=null){
			town=jo.get("town").toString();
		}
		int townbid = 0;
		if(jo.get("townbid")!=null){
			townbid=Integer.valueOf(jo.get("townbid").toString());
		}
		int citybid = 0;
		if(jo.get("citybid")!=null){
			citybid=Integer.valueOf(jo.get("citybid").toString());
		}
		int provincebid = 0;
		if(jo.get("provincebid")!=null){
			provincebid=Integer.valueOf(jo.get("provincebid").toString());
		}
		int countybid = 0;
		if(jo.get("countybid")!=null){
			countybid=Integer.valueOf(jo.get("countybid").toString());
		}
		
		String defStatus="0";
		
		ResultObject res = new ResultObject();
		
		ZtAddr ztAddr=new ZtAddr();
		ztAddr.setId(addrid);
		ztAddr=ztAddrService.get(ztAddr);
		if(jo.get("defStatus")!=null){
			defStatus=jo.get("defStatus").toString();
		}
		if("1".equals(defStatus)) {
			ztAddrService.setAllStatus(ztAddr);
			ztAddr.setDefStatus("1");
		}else {
			ztAddr.setDefStatus("0");
		}
		ztAddr.setArea(area);
		ztAddr.setDetailaddr(detailaddr);
		ztAddr.setAddr(area+" "+detailaddr);
		
		ztAddr.setName(name);
		ztAddr.setPhone(phone);
		ztAddr.setProvince(province);
		ztAddr.setCity(city);
		ztAddr.setCounty(county);
		
		ztAddr.setTown(town);
		ztAddr.setProvincebid(provincebid);
		ztAddr.setCitybid(citybid);
		ztAddr.setCountybid(countybid);
		ztAddr.setTownbid(townbid);
		
		ztAddr.setCreateDate(new Date());
		ztAddrService.save(ztAddr);

		
		res.setData(ztAddr);
		res.setResult(200L);
		Render.renderString(response, res);
	}
	

	/**
	 * 删除数据
	 */

	@RequestMapping(value = "delete")
	@ResponseBody
	public void delete(HttpServletRequest request, HttpServletResponse response) {
		ResultObject res = new ResultObject();
		
		String paramStr=CommonUtils.getParams(request);
		
		JSONObject jo=JSONObject.parseObject(paramStr);
		
		String ztAddrid=jo.get("ztAddrid").toString();
		ZtAddr ztAddr=new ZtAddr();
		ztAddr.setId(ztAddrid);
		ztAddrService.delete(ztAddr);
		
	
		res.setResult(200L);
		Render.renderString(response, res);
	}
	
	
	/**
	 * 删除数据
	 */

	@RequestMapping(value = "setDefStatus")
	@ResponseBody
	public void setDefStatus(HttpServletRequest request, HttpServletResponse response) {
		ResultObject res = new ResultObject();
		
		String paramStr=CommonUtils.getParams(request);
		
		JSONObject jo=JSONObject.parseObject(paramStr);
		String ztAddrid = jo.getString("ztAddrid");
		String adminid = jo.getString("adminid");
		
		ZtAddr ztAddr=new ZtAddr();
		ztAddr.setId(ztAddrid);
		ZtAdmin ztAdmin=new ZtAdmin();
		ztAdmin.setId(adminid);
		ztAddr.setZtAdmin(ztAdmin);
		
		ztAddrService.setDefStatus(ztAddr);
		
		res.setResult(200L);
		Render.renderString(response, res);
	}
	
	@RequestMapping(value = "getFreight")
	@ResponseBody
	public void getFreight(HttpServletRequest request, HttpServletResponse response){
		ResultObject res = new ResultObject();
		
		String paramStr=CommonUtils.getParams(request);
		
		JSONObject jo=JSONObject.parseObject(paramStr);
		JSONArray ztGoods = jo.getJSONArray("ztGoods");
		
		int provincebid = 0;
		if(jo.get("provincebid")!=null){
			provincebid=Integer.valueOf(jo.get("provincebid").toString());
		}
		int citybid = 0;
		if(jo.get("citybid")!=null){
			citybid=Integer.valueOf(jo.get("citybid").toString());
		}
		int countybid = 0;
		if(jo.get("countybid")!=null){
			countybid=Integer.valueOf(jo.get("countybid").toString());
		}
		int townbid = 0;
		if(jo.get("townbid")!=null){
			townbid=Integer.valueOf(jo.get("townbid").toString());
		}
		
		String skus="";
		String free = null;
		BigDecimal sumSupplyPrice = new BigDecimal("0");
		JSONArray jaary=new JSONArray();
		if(ztGoods!=null && ztGoods.size()>0){
			for(int i=0;i<ztGoods.size();i++){
				JSONObject jjb=ztGoods.getJSONObject(i);
				long skuId=Long.valueOf(jjb.get("skuId").toString());
				
				int spotid=Integer.valueOf(jjb.get("spotid").toString());
				ZtGoods goods = new ZtGoods();
				goods.setCode(String.valueOf(skuId));
				goods = ztGoodsService.getByCode(goods);
				if(spotid==1){
					JSONObject jtt=new JSONObject();
					jtt.put("skuId", skuId);
					jtt.put("num",Integer.valueOf(jjb.get("num").toString()));
					jaary.add(jtt);
					skus+=skuId+",";

				}
				sumSupplyPrice.add(new BigDecimal(goods.getSupplyPrice().toString()).multiply(new BigDecimal(jjb.get("num").toString())));//累计协议价
				if(StringUtils.isNotBlank(goods.getNewTalentType())) {
					if (goods.getNewTalentType().contains("1")) {
						free = "0.0";
					}
				}
				
			}
			
			if(StringUtils.isNotBlank(skus)){
			skus=skus.substring(0,skus.length()-1);
			}
		}
		double freight=0d;
		JSONArray jarray=new JSONArray();
		JSONArray resultArray=new JSONArray();
		if(StringUtils.isNotBlank(skus)){
				//检查是否可购
			jarray=ProductPoolUtils.checkAreaLimit(skus,provincebid,citybid,countybid,townbid);//多商品sku就,分隔
			String area=provincebid+"_"+citybid+"_"+countybid;
			
			
			JSONArray jaary_freight=new JSONArray();
			for(int g=0;g<jarray.size();g++){
				JSONObject jh=jarray.getJSONObject(g);
				long skuId=jh.getLong("skuId");
				boolean isAreaRestrict=jh.getBoolean("isAreaRestrict");//false可售
				
				if(!isAreaRestrict){//可售

					for(int i=0;i<jaary.size();i++){
						JSONObject jbbb=jaary.getJSONObject(i);
						long skuid=Long.valueOf(jbbb.get("skuId").toString());
						int num=Integer.valueOf(jbbb.get("num").toString());
						if(skuId==skuid){
							boolean havehuo=ProductPoolUtils.getnewstockbyid(skuid,num,area);
							if(!havehuo){//可售
								resultArray.add(jh);
								jaary_freight.add(jbbb);
								resultArray.add(jh);
							}else{//不可售
								jh.put("isAreaRestrict",true);
								resultArray.add(jh);
							}

							break;
						}



					}
				}else{
					resultArray.add(jh);
				}
				
			}
			
			if(jaary_freight!=null && jaary_freight.size()>0){
				freight=ProductPoolUtils.freight(jaary_freight.toJSONString(),provincebid,citybid,countybid,townbid);
			}
		}
		//ProductPoolUtils.getnewstockbyid(ztGoods.toJSONString(),value+"_"+cityValue+"_"+countyValue);
		if(sumSupplyPrice.doubleValue()>=100){
			free = "0.0";
		}
		res.setData(resultArray);
		if (StringUtil.isNotEmpty(free)){
			res.setMsg(free);
		}else {
			res.setMsg(String.valueOf(freight));
		}
		res.setResult(200L);
		Render.renderString(response, res);
		
	}
	
	//
	@ResponseBody
	@RequestMapping(value = "allProvince")
	public void allProvince(HttpServletRequest request, HttpServletResponse response) {
		ResultObject res = new ResultObject();
		res.setResult(200L);
		JSONArray ja=new JSONArray();
		
		JSONObject jo=new JSONObject();
		jo.put("北京", 1);
		jo.put("重庆", 4);
		jo.put("新疆", 31);
		jo.put("广东", 19);
		jo.put("浙江", 15);
		jo.put("天津", 3);
		jo.put("港澳", 52993);
		jo.put("广西", 20);
		jo.put("内蒙古", 11);
		jo.put("宁夏", 30);
		jo.put("江西", 21);
		jo.put("台湾", 32);
		jo.put("安徽", 14);
		jo.put("贵州", 24);
		jo.put("陕西", 27);
		jo.put("辽宁", 8);
		jo.put("山西", 6);
		jo.put("青海", 29);
		jo.put("四川", 22);
		jo.put("江苏", 12);
		jo.put("河北", 5);
		jo.put("西藏", 26);
		jo.put("钓鱼岛", 84);
		jo.put("福建", 16);
		jo.put("吉林", 9);
		jo.put("云南", 25);
		jo.put("海南", 23);
		jo.put("湖北", 17);
		jo.put("上海", 2);
		jo.put("湖南", 18);
		jo.put("甘肃", 28);
		jo.put("河南", 7);
		jo.put("山东", 13);
		jo.put("黑龙江", 10);
		
		
		Set<Entry<String, Object>> s=jo.entrySet();
		for(Entry<String, Object> b:s){
			JSONObject jb=new JSONObject();
			jb.put("name", b.getKey());
			jb.put("id", b.getValue());
			jb.put("selected",0);
			ja.add(jb);
		}
	
		
		res.setData(ja.toJSONString());
		Render.renderString(response, res);
	}
	
	@ResponseBody
	@RequestMapping(value = "allCity")
	public void allCity(HttpServletRequest request, HttpServletResponse response) {
		ResultObject res = new ResultObject();
		res.setResult(200L);
	
		String paramStr=CommonUtils.getParams(request);
		
		JSONObject jo=JSONObject.parseObject(paramStr);
		int provinceBid = Integer.valueOf(jo.get("provincebid").toString());
		
		JSONObject citys=ProductPoolUtils.citysByProvinceId(provinceBid);
		
		JSONArray ja=new JSONArray();
		Set<Entry<String, Object>> s=citys.entrySet();
		for(Entry<String, Object> b:s){
			JSONObject jb=new JSONObject();
			jb.put("name", b.getKey());
			jb.put("id", b.getValue());
			jb.put("selected",0);
			ja.add(jb);
		}
		res.setData(ja.toJSONString());
		
		Render.renderString(response, res);
	}
	
	@ResponseBody
	@RequestMapping(value = "allCounty")
	public void allCounty(HttpServletRequest request, HttpServletResponse response) {
		ResultObject res = new ResultObject();
		res.setResult(200L);
	
		String paramStr=CommonUtils.getParams(request);
		
		JSONObject jo=JSONObject.parseObject(paramStr);
		int cityBid = Integer.valueOf(jo.get("citybid").toString());
		
		JSONObject citys=ProductPoolUtils.countysByCityId(cityBid);
		
		JSONArray ja=new JSONArray();
		Set<Entry<String, Object>> s=citys.entrySet();
		for(Entry<String, Object> b:s){
			JSONObject jb=new JSONObject();
			jb.put("name", b.getKey());
			jb.put("id", b.getValue());
			jb.put("selected",0);
			ja.add(jb);
		}
		res.setData(ja.toJSONString());
	
		Render.renderString(response, res);
	}
	
	@ResponseBody
	@RequestMapping(value = "allTown")
	public void allTown(HttpServletRequest request, HttpServletResponse response) {
		ResultObject res = new ResultObject();
		res.setResult(200L);
	
		String paramStr=CommonUtils.getParams(request);
		
		JSONObject jo=JSONObject.parseObject(paramStr);
		int countyBid = Integer.valueOf(jo.get("countybid").toString());
		
		JSONObject citys=ProductPoolUtils.townsByCountyId(countyBid);
		
		JSONArray ja=new JSONArray();
		if(citys!=null){
		Set<Entry<String, Object>> s=citys.entrySet();
		for(Entry<String, Object> b:s){
			JSONObject jb=new JSONObject();
			jb.put("name", b.getKey());
			jb.put("id", b.getValue());
			jb.put("selected",0);
			ja.add(jb);
		}
		}
		res.setData(ja.toJSONString());
		Render.renderString(response, res);
	}

}